package com.sk.weichat.adapter;

import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.entity.MultiItemEntity;
import com.mikhaellopez.circularprogressbar.CircularProgressBar;
import com.sk.weichat.R;
import com.sk.weichat.bean.DateBean;
import com.sk.weichat.helper.AvatarHelper;
import com.sk.weichat.ui.mucfile.bean.MucFileBean;
import com.sk.weichat.util.GetFileSizeUtil;

import java.text.SimpleDateFormat;
import java.util.List;

import static com.sk.weichat.ui.mucfile.DownManager.STATE_DOWNLOADED;
import static com.sk.weichat.ui.mucfile.DownManager.STATE_DOWNLOADFAILED;
import static com.sk.weichat.ui.mucfile.DownManager.STATE_DOWNLOADING;
import static com.sk.weichat.ui.mucfile.DownManager.STATE_PAUSEDOWNLOAD;
import static com.sk.weichat.ui.mucfile.DownManager.STATE_UNDOWNLOAD;
import static com.sk.weichat.ui.mucfile.DownManager.STATE_WAITINGDOWNLOAD;

public class MucFileListAdapter extends BaseMultiItemQuickAdapter<MultiItemEntity, BaseViewHolder> {

    public MucFileListAdapter(List<MultiItemEntity> data) {
        super(data);
        addItemType(0, R.layout.item_muc_file);
        addItemType(1, R.layout.item_muc_file_date);
    }

    private static SimpleDateFormat sdf = new SimpleDateFormat("M/dd");

    @Override
    protected void convert(BaseViewHolder holder, MultiItemEntity item) {
        holder.addOnClickListener(R.id.iv_avatar, R.id.iv_doc_type_icon, R.id.tv_doc_name, R.id.tv_bg, R.id.tv_doc_size, R.id.iv_delete, R.id.iv_download,R.id.progressBar);

        switch (holder.getItemViewType()) {
            //文件
            case 0:
                MucFileBean mucFileBean = (MucFileBean) item;
                //头像
                String avatar = AvatarHelper.getAvatarUrl(mucFileBean.getUserId(), true);

//
                AvatarHelper.getInstance().displayUrl(avatar, (ImageView) holder.getView(R.id.iv_avatar),R.drawable.avatar_normal);

                //昵称
                holder.setText(R.id.tv_name, mucFileBean.getNickname());
                //
                String fileName = mucFileBean.getName();
                String type = "";
                try {
                    type = fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length()).toLowerCase();
                } catch (Exception e) {
                }
                if (type.equals("jpg") || type.equals("jpeg") || type.equals("png")) {
                    Glide.with(mContext).load(mucFileBean.getUrl()).centerCrop().placeholder(R.mipmap.default_error).error(R.mipmap.default_error).into((ImageView) holder.getView(R.id.iv_doc_type_icon));
                } else {
                    AvatarHelper.getInstance().fillFileView(type, holder.getView(R.id.iv_doc_type_icon));
                }
                try {
                    fileName = mucFileBean.getName().substring(mucFileBean.getName().lastIndexOf("/") + 1);
                } catch (Exception e) {

                }
                holder.setText(R.id.tv_doc_name, fileName);
                //文档大小
                String sizeStr = GetFileSizeUtil.formatFileSize(mucFileBean.getSize());
                holder.setText(R.id.tv_doc_size, sizeStr);
                //设置进度条
                CircularProgressBar circularProgressBar = (CircularProgressBar) holder.getView(R.id.progressBar);
                circularProgressBar.setProgressMax(mucFileBean.getSize());
                circularProgressBar.setProgress(mucFileBean.getProgress());
                //日期
                holder.setText(R.id.tv_time, sdf.format(mucFileBean.getTime() * 1000));

                //判断下载状态
                switch (mucFileBean.getState()) {
                    case STATE_UNDOWNLOAD:
                    case STATE_DOWNLOADFAILED:
                    case STATE_WAITINGDOWNLOAD:
                        holder.getView(R.id.iv_download_finish).setVisibility(View.GONE);
                        holder.getView(R.id.iv_download).setVisibility(View.GONE);
                        holder.getView(R.id.progressBar).setVisibility(View.GONE);
                        break;
                    case STATE_DOWNLOADING:
                        //下载中
                        holder.getView(R.id.progressBar).setVisibility(View.VISIBLE);
                        holder.getView(R.id.iv_download).setVisibility(View.VISIBLE);
                        holder.setImageResource(R.id.iv_download, R.mipmap.icon_group_suspend);

                        break;
                    case STATE_PAUSEDOWNLOAD:
                        //暂停
                        holder.getView(R.id.progressBar).setVisibility(View.VISIBLE);
                        holder.getView(R.id.iv_download).setVisibility(View.VISIBLE);
                        holder.setImageResource(R.id.iv_download, R.mipmap.icon_group_renewal);
                        break;
                    case STATE_DOWNLOADED:
                        //下载完成
                        holder.getView(R.id.iv_download_finish).setVisibility(View.VISIBLE);
                        holder.getView(R.id.iv_download).setVisibility(View.GONE);
                        holder.getView(R.id.progressBar).setVisibility(View.GONE);
                        break;
                }
                break;


            //日期
            case 1:
                DateBean dateBean = (DateBean) item;
                holder.setText(R.id.tv_date, dateBean.getDate());
                break;
        }
    }

}
